package app;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class UIFrame extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PlayerPanel panel1, panel2, panel3, panel4;
	private JLabel info1Label, info2Label, versionLabel, copyrightLabel, eventLabel, actionLabel;
	private JButton resetButton, startButton;
	
	private LoadedImage image;
	
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	
	
	public UIFrame () {

		

		
		initializeComponents();
		
		
		this.getContentPane().setBackground(Color.LIGHT_GRAY);
		
		try {
			ClassLoader cldr = this.getClass().getClassLoader();
			URL imageURL = cldr.getResource("windowIcon.png");
			ImageIcon b = new ImageIcon(imageURL);
			setIconImage(b.getImage());
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		setTitle("Tore Trinken 1.2.0");
		setVisible(true);
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);
		
	}

	private void initializeComponents() {
		
		layout = new GridBagLayout();
		constraints = new GridBagConstraints();
		this.getContentPane().setLayout(layout);
		
		int[] widths = new int[] {70, 70, 70, 35, 35, 150, 150};
		layout.columnWidths = widths;
		int[] heights = new int[] {14, 14, 245, 20, 25};
		layout.rowHeights = heights;

		info1Label = new JLabel("Zum Starten bitte Namen eintragen und mit \"Go!\" best\u00E4tigen.");
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		layout.setConstraints(info1Label, constraints);
		this.getContentPane().add(info1Label);
		constraints = new GridBagConstraints();
		
		
		info2Label = new JLabel("Um ein neues Spiel zu starten, den \"Reset\" Button dr\u00FCcken.");
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		layout.setConstraints(info2Label, constraints);
		this.getContentPane().add(info2Label);
		constraints = new GridBagConstraints();
		
		resetButton = new JButton("Reset");
		resetButton.setEnabled(false);
		constraints.gridx = 5;
		constraints.gridy = 0;
		constraints.gridheight = 2;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.insets = new Insets(0, 20, 0, 5);
		layout.setConstraints(resetButton, constraints);
		resetButton.addActionListener(this);
		resetButton.setActionCommand("Reset");
		this.getContentPane().add(resetButton);
		constraints = new GridBagConstraints();
		
		startButton = new JButton("Go!");
		startButton.setEnabled(true);
		constraints.gridx = 6;
		constraints.gridy = 0;
		constraints.gridheight = 2;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.insets = new Insets(0, 0, 0, 20);
		layout.setConstraints(startButton, constraints);
		startButton.addActionListener(this);
		startButton.setActionCommand("Go!");
		this.getContentPane().add(startButton);
		constraints = new GridBagConstraints();
		
		panel1 = new PlayerPanel("Spieler 1", true);
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridheight = 2;
		layout.setConstraints(panel1, constraints);
		this.getContentPane().add(panel1);
		constraints = new GridBagConstraints();
		
		panel2 = new PlayerPanel("Spieler 2", false);
		constraints.gridx = 1;
		constraints.gridy = 2;
		constraints.gridheight = 2;
		layout.setConstraints(panel2, constraints);
		this.getContentPane().add(panel2);
		constraints = new GridBagConstraints();
		
		panel3 = new PlayerPanel("Spieler 3", false);
		constraints.gridx = 2;
		constraints.gridy = 2;
		constraints.gridheight = 2;
		layout.setConstraints(panel3, constraints);
		this.getContentPane().add(panel3);
		constraints = new GridBagConstraints();
		
		panel4 = new PlayerPanel("Spieler 4", false);
		constraints.gridx = 3;
		constraints.gridy = 2;
		constraints.gridheight = 2;
		constraints.gridwidth = 2;
		layout.setConstraints(panel4, constraints);
		this.getContentPane().add(panel4);
		constraints = new GridBagConstraints();
		
		versionLabel = new JLabel("Version 1.2.1 (05.04.2012)");
		versionLabel.setForeground(new Color(105, 105, 105));
		versionLabel.setFont(new Font("Tahoma", Font.PLAIN, 8));
		constraints.gridx = 0;
		constraints.gridy = 4;
		constraints.anchor = GridBagConstraints.SOUTHWEST;
		layout.setConstraints(versionLabel, constraints);
		this.getContentPane().add(versionLabel);
		constraints = new GridBagConstraints();
		
		copyrightLabel = new JLabel("Copyright \u00A9 2012 TS, JLS. All Rights Reserved.");
		copyrightLabel.setFont(new Font("Tahoma", Font.PLAIN, 8));
		copyrightLabel.setForeground(new Color(105, 105, 105));
		constraints.gridx = 4;
		constraints.gridy = 4;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.SOUTHEAST;
		layout.setConstraints(copyrightLabel, constraints);
		this.getContentPane().add(copyrightLabel);
		constraints = new GridBagConstraints();
		
		eventLabel = new JLabel();
		eventLabel.setFont(new Font("Arial", Font.PLAIN, 22));
		eventLabel.setForeground(Color.WHITE);
		constraints.gridx = 5;
		constraints.gridy = 1;
		constraints.gridheight = 3;
		constraints.gridwidth = 2;
		layout.setConstraints(eventLabel, constraints);
		this.getContentPane().add(eventLabel);
		constraints = new GridBagConstraints();
		
		
		try {
			image = new LoadedImage("FootballDisabled.png");
			constraints.gridx = 5;
			constraints.gridy = 1;
			constraints.gridheight = 3;
			constraints.gridwidth = 2;
			layout.setConstraints(image, constraints);
			this.getContentPane().add(image);
			constraints = new GridBagConstraints();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		actionLabel = new JLabel();
		actionLabel.setFont(new Font("Arial", Font.BOLD, 12));
//		actionLabel.setForeground(Color.BLACK);
		constraints.gridx = 5;
		constraints.gridy = 2;
		constraints.gridheight = 2;
		constraints.gridwidth = 2;
		constraints.anchor = GridBagConstraints.SOUTH;
		layout.setConstraints(actionLabel, constraints);
		this.getContentPane().add(actionLabel);
		constraints = new GridBagConstraints();
		
		
	}
	
	public void startGame () {


		if ((!panel1.textField.getText().isEmpty()) | 
				(!panel2.textField.getText().isEmpty()) | 
				(!panel3.textField.getText().isEmpty()) | 
				(!panel4.textField.getText().isEmpty())) {
			resetButton.setEnabled(true);
			startButton.setEnabled(false);
			panel1.validateForGame();
			panel2.validateForGame();
			panel3.validateForGame();
			panel4.validateForGame();
		}

	}
	
	public void reset () {

		panel1.resetTo("Spieler 1");
		panel2.resetTo("Spieler 2");
		panel3.resetTo("Spieler 3");
		panel4.resetTo("Spieler 4");
		resetButton.setEnabled(false);
		startButton.setEnabled(true);
		eventLabel.setText("");
		actionLabel.setText("");
	}
	
	public void triggerEvent (Player player, Component parentComponent, String message, String amount) {
		
		player.setCount(player.getCount() + Integer.parseInt(amount));
		eventLabel.setText(player.getName());
		actionLabel.setText(message.substring(2));
//		JOptionPane.showMessageDialog(parentComponent, player.getName() + message,"Trinken!", JOptionPane.ERROR_MESSAGE);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		switch (e.getActionCommand().charAt(0)) {
		case 'G':
			startGame();
			break;
		case 'R':
			reset();
		default:
			break;
		}
		
	}
	
}
